package com.example.campus_activity_backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.domain.Course;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.CourseService;
import com.example.campus_activity_backend.mapper.CourseMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Hongxia
* @description 针对表【t_course】的数据库操作Service实现
* @createDate 2023-11-17 14:13:10
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService {

    @Override
    public long addCourse(Course course) {
        //参数校验
        if(course == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String courseName = course.getCourseName();
        if(courseName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"课程名为空");
        }
        Integer activityId = course.getActivityId();
        if(activityId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"关联活动id为空");
        }
        course.setCreatedTime(new Date());
        //添加数据库
        boolean saveResult = this.save(course);
        if(!saveResult){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"课程创建失败");
        }
        return course.getId();
    }
}




